// pages/components/cancle_orde/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: ''
    },
    info:{
      type: Object,
      value: {}
    },
    list:{
      type: Array,
      value: []
    },
    reason:{
      type: Object,
      value: {}
    }
  },
  observers:{
    reason:function(obj){
      var arr = [];
      for(let i in obj) {
        console.log(i,obj[i])
        if(i == 1){
          arr.push ({
            name: obj[i],
            checked: true
          })
        }else{
          arr.push ({
            name: obj[i],
            checked: false
          })
        }
      }
      this.setData({
        reasonList:arr,
        explain: arr[0]?.name || ''
      })
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    reasonList: [{
        name: '商品缺货',
        checked: true
      },
      {
        name: '未按时间送达',
        checked: false
      },
      {
        name: '自定义描述',
        checked: false
      },
    ],
    explain:'商品缺货',
    checkedAll: false,
    step: 1,
    desc:'',
    items:[]
  },
  ready() {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    closeReason() {
      this.triggerEvent('close')
    },
    sendUp() {
      let reason = this.data.explain
      if(this.data.explain == '其他'){
        reason = this.data.desc ? this.data.desc:  this.data.explain
      }
      this.triggerEvent('send',{
        type: 1,
        reason: reason,
        items: JSON.stringify(this.data.items)
      })
    },
    toStep(){
      let list = this.data.list
      let items = []
      list.forEach(function (o, i) {
        if (o.checked) {
          items.push({
            id: o.id,
            num: o.nums
          })
        }
      })
      this.setData({
        items
      })
      if(items.length<=0){
        getApp().toast('请至少选择一件商品')
        return
      }
      this.setData({
        step: 2
      })
    },
    checkboxChangeReason(e){
      let index = e.currentTarget.dataset.index
      let reasonList = this.data.reasonList
      reasonList[index].checked = !reasonList[index].checked
      
      reasonList.forEach(function (o, i) {
        if (i != index) {
          o.checked = false
        }
      })
      this.setData({
        reasonList,
        explain:reasonList[index].name
      })
    },
    // 输入框操作
    inputWrite(e) {
      console.log(e,this.data.reasonList)
      this.setData({
        desc: e.detail.value
      })
    },
    /**
     * 单个选择商品
     */
    checkboxChange(e) {
      const list = this.data.list;
      const index = e.currentTarget.dataset.index;
      list[index].checked = !list[index].checked;
      let checkedAll = true;
      for (let i = 0; i < list.length; i++) {
        if(list[i].can_return == 1){
          if (!list[i].checked) {
            checkedAll = false;
            break;
          }
        }else{
          checkedAll = true
        }
      }
      this.setData({
          list: list,
          checkedAll: checkedAll,
      });
    },
    /**
     * 选择全部
     */
    checkedAllTap() {
        const checkedAlls = !this.data.checkedAll;
        console.log(checkedAlls)
        const list = this.data.list;
        for (let i = 0; i < list.length; i++) {
          if(list[i].can_return == 1){
            list[i].checked = checkedAlls;
          }else{
            list[i].checked = false;
            // checkedAlls = false;
          }
        }
        this.setData({
            list,
            checkedAll:checkedAlls,
        });
    },
  }
})